//
//  XSDataSaveManager.m
//  XSDataBaseDemo
//
//  Created by 大帅 on 2023/12/19.
//

#import "XSDataSaveManager.h"

@implementation XSDataSaveManager

static XSDataSaveManager *_sharedSingleton = nil;

+ (instancetype)sharedManager {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        // 不能再使用 alloc 方法
        // 因为已经重写了 allocWithZone 方法，所以这里要调用父类的分配空间的方法
        _sharedSingleton = [[super allocWithZone:NULL] init];
    });
    return _sharedSingleton;
}

// 防止 [[A alloc] init] 和 new 引起的错误。因为 [[A alloc] init] 和 new 实际是一样的工作原理，都是执行了下面方法
+ (instancetype)allocWithZone:(struct _NSZone *)zone {
    return [XSDataSaveManager sharedManager];
}

// NSCopying 防止 copy 引起的错误。当你的单例类不遵循 NSCopying 协议，外部调用本身就会出错.
- (id)copyWithZone:(nullable NSZone *)zone {
    return [XSDataSaveManager sharedManager];
}

// 防止 mutableCopy 引起的错误，当你的单例类不遵循 NSMutableCopying 协议，外部调用本身就会出错.
- (id)mutableCopyWithZone:(nullable NSZone *)zone {
    return [XSDataSaveManager sharedManager];
}

-(instancetype)init {
    if (self = [super init]) {
        // 建表
        if (![[JQFMDB shareDatabase] jq_isExistTable:XS_USER_DOWNLOAD]) {
            [[JQFMDB shareDatabase] jq_createTable:XS_USER_DOWNLOAD dicOrModel:[XSTestModel class]];
            NSLog(@"建表成功");
        }
    }
    return self;
}

/// 新增一条数据
- (void)addDownloadSingleData:(XSTestModel *)model {
    //异步(防止UI卡死)插入一条数据, 也同样可以使用线程安全的方法(在jq_inDatabase的block中插入)
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [[JQFMDB shareDatabase] jq_insertTable:XS_USER_DOWNLOAD dicOrModel:model];
    });
}

/// 新增多条数据
- (void)addDownloadMoreData:(NSArray *)list {
    //异步(防止UI卡死)操作数据
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [[JQFMDB shareDatabase] jq_insertTable:XS_USER_DOWNLOAD dicOrModelArray:list];
    });
}

/// 删除指定数据
- (void)deleteDownloadSingleData:(XSTestModel *)model {
    //异步(防止UI卡死)操作数据
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        //删除指定一条数据
        [[JQFMDB shareDatabase] jq_deleteTable:XS_USER_DOWNLOAD whereFormat:[NSString stringWithFormat:@"WHERE userId = %@", model.userId]];
    });
}

/// 删除全部数据
- (void)deleteDownloadAllData {
    //异步(防止UI卡死)操作数据
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [[JQFMDB shareDatabase] jq_deleteAllDataFromTable:XS_USER_DOWNLOAD];
    });
}

/// 修改指定数据
- (void)updateDownloadSingleData:(XSTestModel *)model {
    //异步(防止UI卡死)操作数据
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [[JQFMDB shareDatabase] jq_updateTable:XS_USER_DOWNLOAD dicOrModel:model whereFormat:[NSString stringWithFormat:@"WHERE userId = %@", model.userId]];
    });
}

/// 查看指定数据
- (XSTestModel *)lookDownloadSingleData:(XSTestModel *)model {
    NSArray *tempArr = [[JQFMDB shareDatabase] jq_lookupTable:XS_USER_DOWNLOAD dicOrModel:XSTestModel.class whereFormat:[NSString stringWithFormat:@"WHERE userId = %@", model.userId]];
    if (tempArr.count > 0) {
        return tempArr.firstObject;
    }
    return nil;
}

/// 查看全部数据
- (NSArray *)lookDownloadAllData {
    return [[JQFMDB shareDatabase] jq_lookupTable:XS_USER_DOWNLOAD dicOrModel:XSTestModel.class whereFormat:nil];
}


@end
